package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.interagriculture.common.Constants;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.Journalism;
import com.interagriculture.entity.JournalismMain;
import com.interagriculture.entity.vo.JournalismDetailVO;
import com.interagriculture.entity.vo.JournalismVO;
import com.interagriculture.mapper.JournalismMainMapper;
import com.interagriculture.mapper.JournalismMapper;
import com.interagriculture.service.JournalismMainService;
import com.interagriculture.service.JournalismService;
import com.interagriculture.utils.StringTools;
import com.interagriculture.utils.ThreadLocalUtil;
import com.interagriculture.utils.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Journalism)表服务实现类
 *
 * @author makejava
 * @since 2024-11-14 15:52:14
 */
@Service("journalismService")
public class JournalismServiceImpl implements JournalismService {
    @Resource
    private JournalismMapper journalismMapper;
    @Resource
    private JournalismMainMapper journalismMainMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Journalism queryById(Integer id) {
        return this.journalismMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param journalism 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Journalism> queryByPage(Journalism journalism, PageRequest pageRequest) {
        long total = this.journalismMapper.count(journalism);
        return new PageImpl<>(this.journalismMapper.queryAllByLimit(journalism, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param journalism 实例对象
     * @return 实例对象
     */
    @Override
    public Journalism insert(Journalism journalism) {
        this.journalismMapper.insert(journalism);
        return journalism;
    }

    /**
     * 修改数据
     *
     * @param journalism 实例对象
     * @return 实例对象
     */
    @Override
    public Journalism update(Journalism journalism) {
        this.journalismMapper.update(journalism);
        return this.queryById(journalism.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        journalismMapper.deleteById(id);
        List<JournalismMain> journalismMainList = journalismMainMapper.selectPictureByJournalismId(id);
        for(JournalismMain journalismMain:journalismMainList){
                uploadFileUtil.removePicture(Constants.JOURNALISM,journalismMain.getId(),journalismMain.getPicture());
        }
        journalismMainMapper.deleteByJournalismId(id);
        return this.journalismMapper.deleteById(id) > 0;
    }

    /**
     * 新增或修改数据
     * @param journalism
     */
    @Override
    public Integer saveOrUpdate(Journalism journalism) {
        if(journalism.getId()==null){
            journalism.setTime(new Date());
            journalism.setUploader(ThreadLocalUtil.get().getAccount());
        }else {
            //上传作者不改变
            journalism.setUploader(journalismMapper.queryById(journalism.getId()).getUploader());
        }
        journalismMapper.insertOrUpdate(journalism);

        return journalism.getId();
    }

    @Override
    public PageResult queryJournalismByPage(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("time desc");
        List<JournalismVO> journalisms = journalismMapper.queryAllByPage();
        PageInfo<JournalismVO> pages = new PageInfo<>(journalisms);
        return new PageResult(pages.getTotal(),pages.getPages(),pages.getList());
    }

    @Override
    public JournalismDetailVO queryDetailJournalismById(Integer id) {
        Journalism journalism = journalismMapper.queryById(id);
        PageHelper.orderBy("id asc");
        List<JournalismMain> journalismMainList = journalismMainMapper.selectByJournalismId(id);
        JournalismDetailVO journalismDetailVO = new JournalismDetailVO();
        journalismDetailVO.setId(id);
        journalismDetailVO.setTitle(journalism.getTitle());
        journalismDetailVO.setTime(journalism.getTime());
        journalismDetailVO.setJournalismMainList(journalismMainList);
        return journalismDetailVO;
    }
}
